package vip.mall.modular.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.mall.common.result.CommonPageResult;
import vip.mall.modular.user.entity.User;
import vip.mall.modular.user.mapper.UserMapper;
import vip.mall.modular.user.params.AddUserParam;
import vip.mall.modular.user.params.EditUserPasswordParams;
import vip.mall.modular.user.params.UserPageParams;
import vip.mall.modular.user.service.UserService;

import java.util.List;

/**
 * @author Asus
 * @Date 2025/2/10 10:12
 * @Descripition
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public boolean add(AddUserParam addUserParam) {
        User user= BeanUtil.copyProperties(addUserParam,User.class);
        if (ObjectUtil.isEmpty(user.getPassword()))
            user.setPassword("123456");
        user.setRoleFlag(0);
        return save(user);
    }

    @Override
    public CommonPageResult pageList(UserPageParams userPageParams) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(userPageParams.getUsername()))
            lambdaQueryWrapper.like(User::getUsername,userPageParams.getUsername());
        Page<User> page = new Page<>(userPageParams.getPageNum(), userPageParams.getPageSize());
        Page<User> userPage = this.page(page, lambdaQueryWrapper);
        if (ObjectUtil.isNotNull(userPage))
            return CommonPageResult.builder()
                    .data(userPage.getRecords())
                    .pageNum((int) userPage.getCurrent())
                    .pageSize((int) userPage.getSize())
                    .total(userPage.getTotal())
                    .totalPage((int) userPage.getPages())
                    .build();
        return null;
    }
    @Override
    public CommonPageResult selectDeleteUser(UserPageParams userPageParams) {
        int pageStart = 0;
        if (userPageParams.getPageNum()>1)
            pageStart=(userPageParams.getPageNum()-1)*userPageParams.getPageSize();
        List<User> selectDeleteUser = baseMapper.selectDeleteUser(userPageParams, pageStart, userPageParams.getPageSize());
        return CommonPageResult.builder()
                .data(selectDeleteUser)
                .pageNum(userPageParams.getPageNum())
                .pageSize(userPageParams.getPageSize())
                .total(baseMapper.selectDeleteUserCount(userPageParams))
                .totalPage((int) Math.ceil(baseMapper.selectDeleteUserCount(userPageParams)*1.0/userPageParams.getPageSize()))
                .build();
    }
    @Override
    public boolean delete(String userId) {
        return baseMapper.deleteUser(userId);
    }

    @Override
    public User getUserInfo(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(username))
            lambdaQueryWrapper.eq(User::getUsername,username);
        return getOne(lambdaQueryWrapper);
    }

    @Override
    public boolean editUserPassword(EditUserPasswordParams editUserPasswordParams) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId,editUserPasswordParams.getId());
        lambdaQueryWrapper.eq(User::getPassword,editUserPasswordParams.getOldPassword());
        User one = getOne(lambdaQueryWrapper);
        if (ObjectUtil.isNotNull(one)){
            one.setPassword(editUserPasswordParams.getNewPassword());
            return updateById(one);
        }
        return false;
    }

}
